package com.xy.annotaion.handler.aop.config;

import com.xy.annotaion.handler.aop.advisor.TestAdvisor;
import com.xy.annotaion.handler.aop.bean.TestAopBean;
import com.xy.annotaion.handler.aop.interceptor.TestMethodInterceptor;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Dell
 */
@Configuration
public class TestAopConfig {


    /**
     * 这个advisor会变成全局的拦截器，一开始就生效，十分危险
     *
     * @return
     */
//    @Bean
    public TestAdvisor testAdvisor() {
        return new TestAdvisor();
    }


//    @Bean("testAopBean")
//    public ProxyFactoryBean proxyFactoryBean() {
@Bean
public ProxyFactoryBean testAopBean() {

//        ProxyFactory proxyFactory = new ProxyFactory();
//        proxyFactory.setAopProxyFactory(new DefaultAopProxyFactory());
//        proxyFactory.setTarget(new TestAopBean());

    ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
        proxyFactoryBean.setInterceptorNames("testAdvisor");
    proxyFactoryBean.setTarget(new TestAopBean());
//        proxyFactoryBean.setTargetName("");
//        proxyFactoryBean.setTargetSource();
    proxyFactoryBean.setProxyTargetClass(true);
//    proxyFactoryBean.setTargetClass(TestAopBean.class);
//        proxyFactoryBean.setOptimize(true);
    proxyFactoryBean.addAdvice(new TestMethodInterceptor());
//        proxyFactoryBean.setSingleton(true);
    return proxyFactoryBean;
}


}
